import Cesium from '../Ces/Cesium';
import EmptyImageryProvider from './EmptyImageryProvider';
import fragmentShaderSourceBlend from '../Shaders/fragmentShaderSourceBlend'
import { ImageryProvider } from 'cesium';

class CommonLayers {
  static _Base_World() {
    return new EmptyImageryProvider()
  }

  static CesT_url = '';
  static CesT_requestVertexNormals = true;
  static CesT_requestWaterMask = true;

  static _CesT() {
    const url = CommonLayers.CesT_url;

    const terrainLayer = Cesium.CesiumTerrainProvider.fromUrl(url, {
      requestVertexNormals: CommonLayers.CesT_requestVertexNormals,
      requestWaterMask: CommonLayers.CesT_requestWaterMask,
    });

    return terrainLayer;
  }

  static get CesT_VM() {
    return new Cesium.ProviderViewModel({
      name: '高程',
      tooltip: '',
      iconUrl: Cesium.buildModuleUrl('Widgets/Images/TerrainProviders/CesiumWorldTerrain.png'),
      creationFunction: CommonLayers._CesT,
    });
  }

  static get Empty_DemVM() {
    return new Cesium.ProviderViewModel({
      name: '无高程',
      tooltip: '',
      iconUrl: Cesium.buildModuleUrl('Widgets/Images/TerrainProviders/Ellipsoid.png'),
      creationFunction() {
        return new Cesium.EllipsoidTerrainProvider({
          ellipsoid: Cesium.Ellipsoid.WGS84,
        });
      },
    });
  }

  static We_configHillshade = {
    url: 'https://services.arcgisonline.com/arcgis/rest/services/Elevation/World_Hillshade/MapServer/tile/{z}/{y}/{x}'
  }

  static get World_Hillshade_Blend(): ImageryProvider {
    const provider = new Cesium.UrlTemplateImageryProvider({
      ...this.We_configHillshade,
      minimumLevel: 1,
      maximumLevel: 13,
    })
    provider.createComputeCommand = this.createBlendComputeCommand
    return provider
  }


  static createComputeCommand = (_that, texture, fragmentShaderSource) => {
    const context = texture._context
    const uniformMap: any = {}
    return new Cesium.ComputeCommand({
      uniformMap,
      fragmentShaderSource,
      preExecute(command) {
        uniformMap.baseTexture = () => texture
        command.outputTexture = new Cesium.Texture({
          context,
          width: texture.width,
          height: texture.height,
          pixelFormat: texture.pixelFormat,
          pixelDatatype: texture.pixelDatatype,
          preMultiplyAlpha: texture.preMultiplyAlpha,
        })
      },
    })
  }

  static createBlendComputeCommand = (_that, texture) => {
    return this.createComputeCommand(_that, texture, fragmentShaderSourceBlend)
  }
}

export default CommonLayers;
